package com.hoangkien.DAO;


import java.util.ArrayList;

import com.hoangkien.entity.CategoryEntity;
import com.hoangkien.entity.ImageEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CategoryDAO {
	private static final String TAG = "CategoryDAO";
	private final SQLiteDatabase db; 
	
	public CategoryDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_author(CategoryEntity categoryEntity){
		ContentValues value = new ContentValues();
		value.put(CategoryScheme.CATEGORY_ID, categoryEntity.getId());
		value.put(CategoryScheme.CATEGORY_NAME, categoryEntity.getName());
		if(db.insert(CategoryScheme.TABLE_CATEGORY_NAME, null, value) == -1){
			Log.e(TAG,"Bug insert category");
		} else {
			Log.e(TAG,"Successfully insert category");
		}
		return 0;
	}
	
	public ArrayList<CategoryEntity> getAllCategory() {
		ArrayList<CategoryEntity> lisCategoryEntity = new ArrayList<CategoryEntity>();
		String query = "select * from "+CategoryScheme.TABLE_CATEGORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()) {
			do {
				CategoryEntity categoryEntity = new CategoryEntity();
				categoryEntity.setId(cursor.getInt(0));
				categoryEntity.setName(cursor.getString(1));
				lisCategoryEntity.add(categoryEntity);
			} while (cursor.moveToNext());
		}
		return lisCategoryEntity;
	}
}
